<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 构成特定和需要添加的最少元素 -->
    <script>
      var minElements = function (nums, limit, goal) {
        //接收一个函数作为累加器，数组中的每个值（从左到右）开始缩减，最终计算为一个值
        let all = nums.reduce((a, b) => a + b)
        if (all === goal) {
          return 0
        }
        //求绝对值
        let diff = Math.abs(goal - all)
        //返回大于或等于给定数字的最小整数
        return Math.ceil(diff / limit)
      }
      console.log(minElements([1, -1, 1], 3, -4))
    </script>
  </body>
</html>
